package com.sinog.alarm.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.sinog.alarm.model.Alarm;
import com.sinog.alarm.model.LocalCode;
import com.sinog.alarm.model.SystemCode;
import com.sinog.alarm.service.AlarmInsertService;
import com.sinog.alarm.service.AlarmReasonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.Date;
import java.util.List;

/**
 * @desc 将扫描警告数据和插入警告业务结合
 * @author lk
 * @date 2022-02-22 9:41
 */
@Slf4j
@Service
public class InsertResonImpl {

    @Autowired
    private LocalCode localCode;
    @Autowired
    private AlarmInsertService alarmInsertService;
    @Autowired
    private AlarmReasonService alarmReasonService;

    /**
     * 描述
     * @param abc abc
     * @param type type
     * @param remark remark
     */
    @DS("db1")
    @Transactional(rollbackFor = Exception.class)
    public void insertabc(List<SystemCode> abc,String type,String remark) {
        if(!CollectionUtils.isEmpty(abc)) {
            abc.forEach(systemCode -> {
                Alarm alarm = new Alarm();
                alarm.setFlag(localCode.getLocalCode());
                alarm.setFlagName(localCode.getFlagName());
                alarm.setCode(systemCode.getCodeId());
                alarm.setParentCode(systemCode.getPcodeId());
                alarm.setCodeName(systemCode.getCodeName());
                alarm.setType(type);
                alarm.setRemark(remark);
                alarm.setCreateTime(new Date());
                alarmInsertService.insertAlarm(alarm);
            });
        }
    }

    /**
     * 描述
     * @param localCode localCode
     */
    @DS("db1")
    @Transactional(rollbackFor = Exception.class)
    public void deleteAlarmResons(LocalCode localCode) {
        alarmInsertService.deleteAlarmResons(localCode);
    }

    /**
     * 描述
     */
    @DS("db2")
    public void selectAlarmResons() {
        //3、有部门，没有用户
        List<SystemCode> departmentNotHaveUser = alarmReasonService.findDepartmentNotHaveUser();
        log.info("3:{}",departmentNotHaveUser);
        insertabc(departmentNotHaveUser,"3","有部门，没有用户");
        //4 有用户，没有部门
        List<SystemCode> userNotHaveDepartment = alarmReasonService.findUserNotHaveDepartment();
        log.info("4:{}",userNotHaveDepartment);
        insertabc(userNotHaveDepartment,"4","有用户，没有部门");
        //5 有部门，没有tbsys_code
        List<SystemCode> departmentNotHaveSystemCode = alarmReasonService.findDepartmentNotHaveSystemCode();
        log.info("5:{}",departmentNotHaveSystemCode);
        insertabc(departmentNotHaveSystemCode,"5","有部门，没有tbsys_code");
        //6 有tbsys_code，没有部门
        List<SystemCode> systemCodeNotHaveDepartment = alarmReasonService.findSystemCodeNotHaveDepartment();
        log.info("6:{}",systemCodeNotHaveDepartment);
        insertabc(systemCodeNotHaveDepartment,"6","有tbsys_code，没有部门");
        //11 有用户，没有tbsys_code
        List<SystemCode> userNotHaveSystemCode = alarmReasonService.findUserNotHaveSystemCode();
        log.info("11:{}",userNotHaveSystemCode);
        insertabc(userNotHaveSystemCode,"11","有用户，没有tbsys_code");
        //12 有tbsys_code，没有用户
        List<SystemCode> systemCodeNotHaveUser = alarmReasonService.findSystemCodeNotHaveUser();
        log.info("12:{}",systemCodeNotHaveUser);
        insertabc(systemCodeNotHaveUser,"12","有tbsys_code，没有用户");
        //13 有tbsys_user，没有auth_user
        List<SystemCode> systemCodeNotHaveAuthUser = alarmReasonService.findSystemCodeNotHaveAuthUser();
        log.info("13:{}",systemCodeNotHaveAuthUser);
        insertabc(systemCodeNotHaveAuthUser,"13","有tbsys_user，没有auth_user");
        //14 有auth_user，没有tbsys_user
        List<SystemCode> authUserNotHaveSystemCode = alarmReasonService.findAuthUserNotHaveSystemCode();
        log.info("14:{}",authUserNotHaveSystemCode);
        insertabc(authUserNotHaveSystemCode,"14","有auth_user，没有tbsys_user");
        //15 下面两行字典表和用户表结果要一致
        List<SystemCode> integers = alarmReasonService.systemCodeAuthUserCount();
        log.info("15:{}",integers);
        insertabc(integers,"15","下面两行字典表和用户表结果要一致");
        //16禁用的系统用户信息
        List<SystemCode> disabledSystemCodes = alarmReasonService.findDisabledSystemCode();
        log.info("16:{}",disabledSystemCodes);
        insertabc(disabledSystemCodes,"16","禁用的tbsys_user系统用户信息");
        //17禁用的用户认证账号信息
        List<SystemCode> disabledAuthUsers = alarmReasonService.findDisabledAuthUser();
        log.info("17:{}",disabledAuthUsers);
        insertabc(disabledAuthUsers,"17","禁用的用户认证auth_user账号信息");
        //20 查询tbsys_department和tbsys_user上级不一致，不一致的需要排查原因使其一致
        List<SystemCode> udInconsistentVos = alarmReasonService.departmentInconsistentUser();
        log.info("20:{}",udInconsistentVos);
        insertabc(udInconsistentVos,"20","查询tbsys_department和tbsys_user上级不一致，不一致的需要排查原因使其一致");
    }
}